/*
 * @(#)Mnemonic.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.swing.text;

public class Mnemonic
{
	public static final char DEFAULT_CHAR = '\0';
	public static final int DEFAULT_LOC = -1;

	protected String spec;
	protected int mnemonicLoc = DEFAULT_LOC;
    protected char mnemonicChar = DEFAULT_CHAR;
	protected String name;

	public Mnemonic(String spec)
	{
		this();
		setSpec(spec);
	}

    public Mnemonic()
	{
	}

	/**
	 * @param spec text to display, with &amp; preceding character to underline
	 */
	public void setSpec(String spec)
	{
        this.spec = spec;
        if(spec == null) return;

		mnemonicLoc = spec.indexOf("&");
		if(mnemonicLoc == DEFAULT_LOC)
		{
			this.name = spec;
		}else if( mnemonicLoc >= (spec.length()-1) ){
			this.name = spec;
		}else{
			this.mnemonicChar = spec.charAt(mnemonicLoc+1);
			this.name = String.format("%s%s", spec.substring(0, mnemonicLoc), spec.substring(mnemonicLoc+1));
		}
	}

	public String getSpec(){return spec;}
	public char getCharacter(){return mnemonicChar;}
	public int getIndex(){return mnemonicLoc;}
	public String toString(){return name;}
	public String getName(){return name;}
}
